import axios from "axios";
import user from "../store/user";
// import { useUserStore } from "../store/user";

// import store from "../store";
const url = "http://localhost:8888";

const request = axios.create({
  baseURL: url,
  // timeout: 5000,
});

request.interceptors.request.use(
  function (config: any) {
    //在发送请求之前做某事 //请求前
    // const { token } = useUserStore()
    // config.headers.authorization = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MjYsI…zYzfQ.QbrXpTwW6EQxgvcUvt8AjoKRJZeqs_1r3fARhm0yQcM';
    // config.headers['token'] = localStorage.getItem('token')
    
    let token = encodeURIComponent(user.profile.token as string)
    config.headers["Content-Type"]='application/x-www-form-urlencoded'
    config.headers.authorization=token
    console.log(config.headers);
    
    return config; // config
  },
  function (error: any) {
    //请求错误时做些事
    return Promise.reject(error);
  }
);

//添加响应拦截器
request.interceptors.response.use(
  function (response: any) {
    //对响应数据做些事  返回前
    // 服务区 ---- 响应拦截器  ---- 客户端
    // return {name:'zzz'}
    return response;
  },
  function (error: any) {
    //请求错误时做些事
    return Promise.reject(error);
  }
);

export default request;
